// ~ Package Declaration
// ==================================================

package com.wms.data;

// ~ Comments
// ==================================================

/**
 * 
 * 分頁組件.
 * 
 * <pre>
 * 歷史�?���?
 * 2008/12/2 Timothy
 * 	新建檔案
 * </pre>
 * 
 * @author <pre>
 * SD
 * 	Timothy
 * PG
 * 
 * UT
 * 
 * MA
 * </pre>
 * @version $Rev$
 * 
 *          <p/>
 *          $Id$
 * 
 */
public class PaginateModel {

	// ~ Static Fields
	// ==================================================

	public static final String DEFAULT_KEY_NAME = "PaginateModel";
	public static final String PROP_PAGE_NO = "pageNo";
	public static final int DEFAULT_PAGE_NO = 1;
	public static final int DEFAULT_PAGE_SIZE = 20;

	// ~ Fields
	// ==================================================

	/**
	 * 2008/12/2 Timothy 每頁呈現的數據筆數，default 0
	 */
	private int pageSize;

	/**
	 * 2008/12/2 Timothy 當前呈現頁碼，default 1
	 */
	private int pageNo = PaginateModel.DEFAULT_PAGE_NO;

	/**
	 * 2008/12/2 Timothy 數據總筆數，default 0
	 */
	private int totalSize;

	// ~ Constructors
	// ==================================================

	public PaginateModel(int pageSize) {

		this.setPageSize(pageSize);
	}
	
	public PaginateModel(int pageNo, int pageSize) {
		
		this.setPageNo(pageNo);
		this.setPageSize(pageSize);
	}

	// ~ Methods
	// ==================================================

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	獲取 總頁�?
	 * </pre>
	 * 
	 * @return
	 */
	public final int getTotalPage() {

		return (this.getTotalSize() / this.getPageSize()) + ((this.getTotalSize() % this.getPageSize() > 0) ? 1 : 0);
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	查看是否存在上一�?
	 * </pre>
	 * 
	 * @return true �? false 不是
	 */
	public final boolean isHasPreviousPage() {

		return this.getPageNo() > 1;
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	查看是否存在下一�?
	 * </pre>
	 * 
	 * @return true �? false 不是
	 */
	public final boolean isHasNextPage() {

		return this.getPageNo() < this.getTotalPage();
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	查看是否第一�?
	 * </pre>
	 * 
	 * @return true �? false 不是
	 */
	public final boolean isFirstPage() {

		return !this.isHasPreviousPage();
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	查看是否�?���?��
	 * </pre>
	 * 
	 * @return true �? false 不是
	 */
	public final boolean isLastPage() {

		return !this.isHasNextPage();
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	獲取 數據總筆�?
	 * </pre>
	 * 
	 * @return 數據總筆�?
	 */
	public final int getTotalSize() {

		return this.totalSize;
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 *   設定數據總筆�?
	 * </pre>
	 * 
	 * @param totalSize
	 *          要設定的數據總筆�?
	 */
	public final void setTotalSize(int totalSize) {

		if (totalSize < 0) {
			throw new IllegalArgumentException("The totalSize must greater than or equals to 0");
		}

		this.totalSize = totalSize;
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	獲取每頁呈現的數據筆�?
	 * </pre>
	 * 
	 * @return 每頁呈現的數據筆�?
	 */
	public final int getPageSize() {

		return this.pageSize;
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 *   設定每頁呈現的數據筆�?
	 * </pre>
	 * 
	 * @param pageSize
	 *          要設定的每頁呈現的數據筆�?
	 */
	public final void setPageSize(int pageSize) {

		if (pageSize < 0) {
			throw new IllegalArgumentException("The pageSize must greater than or equals to 0");
		}

		this.pageSize = pageSize;
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	獲取當前呈現頁碼
	 * </pre>
	 * 
	 * @return
	 */
	public final int getPageNo() {

		return this.pageNo;
	}

	/**
	 * <pre>
	 * 2008/12/2 Timothy
	 * 	設定 當前呈現頁碼
	 * </pre>
	 * 
	 * @param pageNo
	 *          要設定的 當前呈現頁碼
	 */
	public final void setPageNo(int pageNo) {

		if (pageNo < 1) {
			throw new IllegalArgumentException("The pageNo must greater than 0");
		}

		this.pageNo = pageNo;
	}

}
